#include <iostream>
#include <vector>

int const maxn = 107;
int const maxm = 11;
int f[2][1<<maxm][1<<maxm];
int da[maxn];
int n, m, len, current;

std::vector<int> init_row[maxn]; // possible state for each row
typedef std::vector<int>::iterator s_iter;

void init_row_dfs(int row, int col, int s/*tate*/)
{
	if (col == m) init_row[row].push_back(s);
	if (col >= m) return;

	init_row_dfs(row, col + 1, s * 2);

	if (!(da[row] & (1<<col))) {
		int ts = s * 2 + 1;
		if (col == m-1) init_row_dfs(row, col + 1, ts);
		else
		if (col == m-2) init_row_dfs(row, col + 2, ts * 2);
		else			init_row_dfs(row, col + 3, ts * 4);
	}
}

int bit_count_one(int x)
{
	x = (x & 0x55555555) + ((x>>1) & 0x55555555);
	x = (x & 0x33333333) + ((x>>2) & 0x33333333);
	x = (x & 0x0F0F0F0F) + ((x>>4) & 0x0F0F0F0F);
	x = (x & 0x00FF00FF) + ((x>>8) & 0x00FF00FF);
	x = (x & 0x0000FFFF) + ((x>>16)& 0x0000FFFF);
	return x;
}

bool check(int x, int y, int z)
{
	return !((x & y) || (x & z) || (y & z));
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin >> n >> m;
	len = 1 << m;
	da[0] = da[1] = da[n+2] = len - 1;
	for (int i = 2; i <= n+1; i++) {
		int tmp = 0;
		for (int j = 0; j < m; j++) {
			char ch; std::cin >> ch;
			tmp = tmp * 2 + (ch == 'H');
		}
		da[i] = tmp;
	}

	for (int i = 0; i <= n+2; i++) init_row_dfs(i, 0, 0);

	int ans = 0;
	for (int i = 2; i <= n+2; i++) {
		current ^= 1;
		for (s_iter it = init_row[i-1].begin(); it != init_row[i-1].end(); ++it) {
			int prow = *it;
			for (s_iter it1 = init_row[i].begin(); it1 != init_row[i].end(); ++it1) {
				int row = *it1;
				for (s_iter it0 = init_row[i-2].begin(); it0 != init_row[i-2].end(); ++it0) {
					int pprow = *it0;
					if (check(pprow, prow, row)) {
						f[current][prow][row] = std::max(f[current][prow][row],
								f[current ^ 1][pprow][prow] + bit_count_one(row));
						ans = std::max(ans, f[current][prow][row]);
					}
				}
			}
		}
	}

	std::cout << ans << '\n';
}

